package com.txd.mofa.login;


import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.jiekou.FinishJishi;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.utils.VerificationTimer;

import java.util.ArrayList;
import java.util.Map;

@Layout(R.layout.aty_bangding)
public class BangdingAty extends BaseAty {

    private TextView tv_title,tv_jishi,tv_yanzhengma;
    private ImageView iv_back;
    private EditText et_shoujihao,et_yanzhengma;
    private Button btn;
    private VerificationTimer verificationTimer;

    @Override
    public void initViews() {
        super.initViews();
        tv_jishi = findViewById(R.id.tv_jishi);
        tv_yanzhengma = findViewById(R.id.tv_yanzhengma);
        tv_title = findViewById(R.id.tv_title);
        iv_back = findViewById(R.id.iv_back);
        et_shoujihao = findViewById(R.id.et_shoujihao);
        et_yanzhengma = findViewById(R.id.et_yanzhengma);
        btn = findViewById(R.id.btn);

        et_shoujihao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {



            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_yanzhengma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0 && s != null){
                    btn.setBackground(getResources().getDrawable(R.mipmap.button_huang));
                }else {
                    btn.setBackground(getResources().getDrawable(R.mipmap.button_hei));
                }
            }
        });

        verificationTimer = new VerificationTimer(60000, 1000, tv_jishi, "获取验证码", new FinishJishi() {
            @Override
            public void finish_jishi() {
                tv_jishi.setVisibility(View.GONE);
                tv_yanzhengma.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }


    @Override
    public void initDatas(JumpParameter paramer) {

        String name = paramer.getString("name");

        if (name == null || name.equals("")) {
            name = getIntent().getStringExtra("name");
        }

        tv_title.setText(name);

    }

    @Override
    public void setEvents() {

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_shoujihao.getText().toString().equals("") || et_shoujihao.getText().toString() == null){
                    Toast.makeText(me, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (et_yanzhengma.getText().toString().equals("") || et_yanzhengma.getText().toString() == null){
                        Toast.makeText(me, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    }else {
                        bangding();
                    }
                }

            }
        });


        tv_yanzhengma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_shoujihao.getText().toString().equals("") || et_shoujihao.getText().toString() == null){
                    Toast.makeText(me, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    sendVerify();
                }
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private String type = "";
    private void sendVerify() {
        if (tv_title.getText().toString().equals("绑定手机号")){
            type = "bind";
        }else {
            type = "mod_bind";
        }
        HttpRequest.POST(me, sendVerify, new Parameter().add("account",et_shoujihao.getText().toString())
                .add("send_type",type), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    Toast.makeText(me, map.get("message"), Toast.LENGTH_SHORT).show();
                    if (map.get("code").equals("1")){
                        verificationTimer.start();
                        tv_jishi.setVisibility(View.VISIBLE);
                        tv_yanzhengma.setVisibility(View.GONE);
                    }
                }else {
                    toast("网络异常");
                }
            }
        });
    }

    private void bangding() {
        HttpRequest.POST(me, bindPhone, new Parameter().add("token",token)
                .add("account",et_shoujihao.getText().toString())
                .add("verify",et_yanzhengma.getText().toString())
                .add("send_type",type), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    Toast.makeText(me, map.get("message"), Toast.LENGTH_SHORT).show();
                    if (map.get("code").equals("1")){
                        finish();
                    }else {

                    }
                }else {
                    toast("网络异常");
                }
            }
        });
    }
}
